package com.example.dianproject.controller;

import com.example.dianproject.dto.MeterDTO;
import com.example.dianproject.entity.Meter;
import com.example.dianproject.service.MeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/meters")
public class MeterController {

    @Autowired
    private MeterService meterService;

    @GetMapping("/all")
    public ResponseEntity<List<Meter>> getAllMetersWithDetails() {
        return ResponseEntity.ok(meterService.getAllMetersWithDetails());
    }

    @GetMapping
    public ResponseEntity<List<MeterDTO>> getAllMeters() {
        return ResponseEntity.ok(meterService.getAllMeters());
    }

    @GetMapping("/{id}")
    public ResponseEntity<MeterDTO> getMeterById(@PathVariable("id") Long meterId) {
        return ResponseEntity.ok(meterService.getMeterById(meterId));
    }

    @PostMapping
    public ResponseEntity<MeterDTO> createMeter(@RequestBody MeterDTO meterDTO) {
        return ResponseEntity.ok(meterService.createMeter(meterDTO));
    }

    @PutMapping("/{id}")
    public ResponseEntity<MeterDTO> updateMeter(
            @PathVariable("id") Long meterId,
            @RequestBody MeterDTO meterDTO) {
        return ResponseEntity.ok(meterService.updateMeter(meterId, meterDTO));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMeter(@PathVariable("id") Long meterId) {
        meterService.deleteMeter(meterId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/count")
    public ResponseEntity<Long> getMeterCount() {
        return ResponseEntity.ok(meterService.getMeterCount());
    }
} 